#include "SkyBox.h"
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <SOIL.h>

SkyBox::SkyBox(string dirPath, string left, string back, string right, string front, string top, string bottom)
{
	string filePath;
	filePath = dirPath + left;
	imageId[SKY_LEFT] = SOIL_load_OGL_texture (
		filePath.c_str(),
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | /*SOIL_FLAG_INVERT_Y |*/ SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);

	filePath = dirPath + back;
	imageId[SKY_BACK] = SOIL_load_OGL_texture (
		filePath.c_str(),
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | /*SOIL_FLAG_INVERT_Y |*/ SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);

	filePath = dirPath + right;
	imageId[SKY_RIGHT] = SOIL_load_OGL_texture (
		filePath.c_str(),
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | /*SOIL_FLAG_INVERT_Y |*/ SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);

	filePath = dirPath + front;
	imageId[SKY_FRONT] = SOIL_load_OGL_texture (
		filePath.c_str(),
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | /*SOIL_FLAG_INVERT_Y |*/ SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);

	filePath = dirPath + top;
	imageId[SKY_TOP] = SOIL_load_OGL_texture (
		filePath.c_str(),
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | /*SOIL_FLAG_INVERT_Y |*/ SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);

	filePath = dirPath + bottom;
	imageId[SKY_BOTTOM] = SOIL_load_OGL_texture (
		filePath.c_str(),
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | /*SOIL_FLAG_INVERT_Y |*/ SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);
}

SkyBox::~SkyBox()
{
	glDeleteTextures(6, imageId);
}

void SkyBox::draw(float factor)
{
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_FRONT]);
	glBegin(GL_QUADS);
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor,  1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor,  1.0f * factor);  // Top Left Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_BACK]);
	glBegin(GL_QUADS);
		// Back Face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor, -1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor, -1.0f * factor);  // Bottom Left Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_TOP]);
	glBegin(GL_QUADS);
		// Top Face
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f * factor,  1.0f * factor,  1.0f * factor);  // Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f * factor,  1.0f * factor,  1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Right Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_BOTTOM]);
	glBegin(GL_QUADS);
		// Bottom face
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f * factor, -1.0f * factor, -1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f * factor, -1.0f * factor, -1.0f * factor);  // Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Right Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_RIGHT]);
	glBegin(GL_QUADS);		
		// Right face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor, -1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor,  1.0f * factor);  // Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Left Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_LEFT]);
	glBegin(GL_QUADS);
		// Left Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor, -1.0f * factor);  // Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor,  1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Left Of The Texture and Quad
	glEnd();
	glDisable(GL_TEXTURE_2D);
	glEnable(GL_DEPTH_TEST);
}